package com.njxxted.activiti.modules.base.controller;

import com.alibaba.fastjson.JSONObject;
import com.njxxted.activiti.common.exception.CustomerException;
import com.njxxted.activiti.common.response.ResponseVo;
import com.njxxted.activiti.common.util.FeignUtil;
import com.njxxted.activiti.modules.api.CommonApi;
import com.njxxted.activiti.modules.api.PermissionApi;
import com.njxxted.activiti.modules.api.entity.MenuDto;
import com.njxxted.activiti.modules.api.entity.UserDto;
import com.njxxted.activiti.modules.api.entity.UserVo;
import feign.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Set;

/**
 * 描述：登录/登出 控制器
 * <p>
 * 作者：Ostrich Hu
 * 时间：2019/8/1 10:15 星期四
 */
@RestController
@RequestMapping(value = "/base")
@Api(tags = "用户登录/登出/修改")
public class SysController extends BaseController {

    @PostMapping("/login")
    @ApiOperation(value = "登录", notes = "登录")
    public ResponseVo login(UserVo userVo, HttpServletResponse servletResponse) {
        PermissionApi api = new FeignUtil<PermissionApi>().invokeService(PermissionApi.class);
        Response response = api.login(userVo);
        Set<String> keySets = response.headers().keySet();
        keySets.forEach(key -> {
            Collection<String> kList = response.headers().get(key);
            kList.forEach(val -> {
                servletResponse.setCharacterEncoding("UTF-8");
                servletResponse.setHeader(String.valueOf(key), val);
            });
        });
        return JSONObject.parseObject(response.body().toString(), ResponseVo.class);
    }

    @ApiOperation(value = "登出", notes = "退出系统")
    @GetMapping("/logout")
    public ResponseVo logout(HttpServletRequest request, HttpServletResponse response) {
        PermissionApi api = new FeignUtil<PermissionApi>().invokeService(PermissionApi.class);
        return api.logout();
    }

    @ApiOperation(value = "修改密码", notes = "修改密码")
    @PostMapping("/updPwd")
    public ResponseVo updPwd(UserDto userDto) {
        PermissionApi api = new FeignUtil<PermissionApi>().invokeService(PermissionApi.class);
        return api.updPwd(userDto);
    }

    @PostMapping("/menus/{roleId}")
    @ApiOperation(value = "根据条件获取菜单")
    public ResponseVo getMenus(@PathVariable String roleId) {
        PermissionApi api = new FeignUtil<PermissionApi>().invokeService(PermissionApi.class);
        return api.getMenusAndPermission(roleId);
    }

    @ApiOperation(value = "下载用户头像")
    @GetMapping(value = "/showHead/{code}")
    public void showHead(@PathVariable String code, HttpServletResponse servletResponse) {
        Response response = new FeignUtil<CommonApi>().invokeCommonService(CommonApi.class).findFileByFileCode(code);
        Response.Body body = response.body();
        Set<String> keySets = response.headers().keySet();
        keySets.forEach(key -> {
            Collection<String> kList = response.headers().get(key);
            kList.forEach(val -> servletResponse.setHeader(String.valueOf(key), val));
        });

        try (InputStream inputStream = body.asInputStream();
             OutputStream outputStream = servletResponse.getOutputStream()) {
            byte[] b = new byte[inputStream.available()];
            inputStream.read(b);
            outputStream.write(b);
            outputStream.flush();
        } catch (IOException e) {
            throw new CustomerException("显示头像IO流异常", e);
        }
    }
}
